package com.lhj.utils;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


import lombok.extern.slf4j.Slf4j;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
/*写http的博文：https://blog.csdn.net/wjrong_1/article/details/51114233*/
/**
 * 用于操作http请求的工具类，HttpClient发送GET、POST请求
 */
@Slf4j
public class HttpClientUtils {
	/*返回成功状态码*/
	private static final int SUCCESS_CODE = 200;

	/**
	 * 带参数列表的Get请求
	 * @param url 请求地址
	 * @param nameValuePairs 参数list
	 * @return 响应体
	 */
	public static String sendGet(String url , List<NameValuePair> nameValuePairs){
		CloseableHttpClient client = null;
		CloseableHttpResponse response = null;
		try {
			//创建HttpClient对象
			client = HttpClients.createDefault();
			//创建URIBuilder
			URIBuilder uriBuilder = new URIBuilder(url);
			//设置参数
			uriBuilder.addParameters(nameValuePairs);
			//创建httpget
			HttpGet httpGet = new HttpGet(uriBuilder.build());
			//设置请求头部编码
			httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
			//设置返回编码
			httpGet.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
			try {
				response = client.execute(httpGet);
				int statusCode = response.getStatusLine().getStatusCode();
				log.info("请求成功响应码为："+statusCode);
				if (SUCCESS_CODE == response.getStatusLine().getStatusCode()) {
					//获取返回对象
					HttpEntity entity = response.getEntity();
					String html = EntityUtils.toString(entity);
					return html;
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.info(e.getMessage()+"\n请求失败,请检查请求地址是否正确(需要加http://)！！！");
				return e.getMessage();
			}finally {
				if (response != null) {
					response.close();
				}
				client.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 不带参数的Get请求
	 * @param url 请求地址
	 * @return	响应体字符串
	 */
	public static String sendGet(String url ) {
		CloseableHttpClient client = null;
		CloseableHttpResponse response = null;
		try {
			//创建HttpClient对象
			client = HttpClients.createDefault();
			//创建URIBuilder
			URIBuilder uriBuilder = new URIBuilder(url);
			//创建httpget
			HttpGet httpGet = new HttpGet(uriBuilder.build());
			//设置请求头部编码
			httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
			//设置返回编码
			httpGet.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
			try {
				response = client.execute(httpGet);
				int statusCode = response.getStatusLine().getStatusCode();
				log.info("请求成功响应码为："+statusCode);
				if (SUCCESS_CODE == response.getStatusLine().getStatusCode()) {
					//获取返回对象
					HttpEntity entity = response.getEntity();
					String html = EntityUtils.toString(entity);
					return html;
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.info(e.getMessage()+"\n请求失败,请检查请求地址是否正确(需要加http://)！！！");
				return e.getMessage();
			}finally {
				if (response != null) {
					response.close();
				}
				client.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 不带参数的Post请求
	 * @param url 请求地址
	 * @return	响应体字符串
	 */
	public static String sendPost(String url ) {
		CloseableHttpClient client = null;
		CloseableHttpResponse response = null;
		try {
			//创建HttpClient对象
			client = HttpClients.createDefault();
			//创建URIBuilder
			URIBuilder uriBuilder = new URIBuilder(url);
			//创建httpPost
			HttpPost httpPost = new HttpPost(uriBuilder.build());
			//设置请求头部编码
			httpPost.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
			//设置返回编码
			httpPost.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
			try {
				response = client.execute(httpPost);
				int statusCode = response.getStatusLine().getStatusCode();
				log.info("请求成功响应码为："+statusCode);
				if (SUCCESS_CODE == response.getStatusLine().getStatusCode()) {
					//获取返回对象
					HttpEntity entity = response.getEntity();
					String html = EntityUtils.toString(entity);
					return html;
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.info(e.getMessage()+"\n请求失败,请检查请求地址是否正确(需要加http://)！！！");
				return e.getMessage();
			}finally {
				if (response != null) {
					response.close();
				}
				client.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 不带参数的Post请求
	 * @param url 请求地址
	 * @param JSONData 请求的json数据
	 * @return	响应体字符串
	 */
	public static String sendPost(String url ,String JSONData) {
		CloseableHttpClient client = null;
		CloseableHttpResponse response = null;
		try {
			//创建HttpClient对象
			client = HttpClients.createDefault();
			//创建URIBuilder
			URIBuilder uriBuilder = new URIBuilder(url);
			//创建httpPost
			HttpPost httpPost = new HttpPost(uriBuilder.build());
			//设置请求头部编码
			httpPost.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
			//设置返回编码
			httpPost.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
			//构造请求数据
			StringEntity jsonEntity = new StringEntity(JSONData, ContentType.APPLICATION_JSON);
			httpPost.setEntity(jsonEntity);
			try {
				response = client.execute(httpPost);
				int statusCode = response.getStatusLine().getStatusCode();
				log.info("请求成功响应码为："+statusCode);
				if (SUCCESS_CODE == response.getStatusLine().getStatusCode()) {
					//获取返回对象
					HttpEntity entity = response.getEntity();
					String html = EntityUtils.toString(entity);
					return html;
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.info(e.getMessage()+"\n请求失败,请检查请求地址是否正确(需要加http://)！！！");
				return e.getMessage();
			}finally {
				if (response != null) {
					response.close();
				}
				client.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

/*	public static void main(String[] args) throws Exception {
		Object o = HttpClientUtils.sendPost("http://www.baidu.com","{\"name\":\"zhangsan\", \"age\":20, \"gender\": \"mail\"}");
		System.out.println(o.toString());
	}*/
}
